<?php
namespace Common\Controller;

use Think\Controller;
use Common\Model\ModuleModel;

class BaseController extends Controller {

    public function __construct() {
        parent::__construct();
        if (method_exists($this, '__init')) {
            $this->__init();
        }
    }


    // 保存数据
    public function store(Model $model, array $data, \Closure $callback = null) {
        $ret = $model->store($data);
        //$this->message($ret);
        if ($ret['status'] == 'success' && $callback instanceof \Closure) {
            $callback($ret);
        } else {
            $this->message($ret);
        }
    }

    // 响应消息
    private function message($ret) {
        if ($ret['status'] == 'success') {
            $this->success($ret['message']);
        } else {
            $this->error($ret['message']);
        }
        exit;
    }

    // 分配模块菜单
    public function assignModuleMenu() {
        $model = new ModuleModel();
        $list = $model->select();
        foreach ($list as $key => $val) {
            $list[$key]['actions'] = json_decode($val['actions'], true);
        }
        $this->assign('_modules', $list);
    }
}